<?php namespace Modules\Shop\Http\Controllers;


class BrandController extends Controller
{

    public function getIndex()
    {

        global $_CFG, $ecs, $err, $smarty, $_LANG, $db, $charset;
        /*------------------------------------------------------ */
        //-- INPUT
        /*------------------------------------------------------ */

        /* 获得请求的分类 ID */
        if (\Input::get('id')) {
            $brand_id = intval(\Input::get('id'));
        }
        if (\Input::get('brand')) {
            $brand_id = intval(\Input::get('brand'));
        }
        if (empty($brand_id)) {
            /* 缓存编号 */
            $cache_id = sprintf('%X', crc32($_CFG['lang']));
            if (!\Tpl::$instance->is_cached('brand_list.dwt', $cache_id)) {
                assign_template();
                $position = assign_ur_here('', $_LANG['all_brand']);
                \Tpl::assign('page_title', $position['title']);    // 页面标题
                \Tpl::assign('ur_here', $position['ur_here']);  // 当前位置

                \Tpl::assign('categories', get_categories_tree()); // 分类树
                \Tpl::assign('helps', get_shop_help());       // 网店帮助
                \Tpl::assign('top_goods', get_top10());           // 销售排行

                \Tpl::assign('brand_list', get_brands());
            }
            \Tpl::display('brand_list.dwt', $cache_id);
            exit();
        }

        /* 初始化分页信息 */
        $page = \Input::get('page') && intval(\Input::get('page')) > 0 ? intval(\Input::get('page')) : 1;
        $size = !empty($_CFG['page_size']) && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 10;
        $cate = \Input::get('cat') && intval(\Input::get('cat')) > 0 ? intval(\Input::get('cat')) : 0;

        /* 排序、显示方式以及类型 */
        $default_display_type = $_CFG['show_order_type'] == '0' ? 'list' : ($_CFG['show_order_type'] == '1' ? 'grid' : 'text');
        $default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
        $default_sort_order_type = $_CFG['sort_order_type'] == '0' ? 'goods_id' : ($_CFG['sort_order_type'] == '1' ? 'shop_price' : 'last_update');

        $sort = (is_set(\Input::get('sort')) && in_array(trim(strtolower(\Input::get('sort'))),
                array('goods_id', 'shop_price', 'last_update'))) ? trim(\Input::get('sort')) : $default_sort_order_type;
        $order = (is_set(\Input::get('order')) && in_array(trim(strtoupper(\Input::get('order'))),
                array('ASC', 'DESC'))) ? trim(\Input::get('order')) : $default_sort_order_method;
        $display = (is_set(\Input::get('display')) && in_array(trim(strtolower(\Input::get('display'))),
                array('list', 'grid', 'text'))) ? trim(\Input::get('display')) : (isset($_COOKIE['ECS']['display']) ? $_COOKIE['ECS']['display'] : $default_display_type);
        $display = in_array($display, array('list', 'grid', 'text')) ? $display : 'text';
        setcookie('ECS[display]', $display, gmtime() + 86400 * 7);

        /*------------------------------------------------------ */
        //-- PROCESSOR
        /*------------------------------------------------------ */

        /* 页面的缓存ID */
        $cache_id = sprintf('%X',
            crc32($brand_id . '-' . $display . '-' . $sort . '-' . $order . '-' . $page . '-' . $size . '-' . $_SESSION['user_rank'] . '-' . $_CFG['lang'] . '-' . $cate));

        if (!\Tpl::is_cached('brand.dwt', $cache_id)) {
            $brand_info = get_brand_info($brand_id);

            if (empty($brand_info)) {
                ecs_header("Location: ./\n");
                exit;
            }

            \Tpl::assign('data_dir', DATA_DIR);
            \Tpl::assign('keywords', htmlspecialchars($brand_info['brand_desc']));
            \Tpl::assign('description', htmlspecialchars($brand_info['brand_desc']));

            /* 赋值固定内容 */
            assign_template();
            $position = assign_ur_here($cate, $brand_info['brand_name']);
            \Tpl::assign('page_title', $position['title']);   // 页面标题
            \Tpl::assign('ur_here', $position['ur_here']); // 当前位置
            \Tpl::assign('brand_id', $brand_id);
            \Tpl::assign('category', $cate);

            \Tpl::assign('categories', get_categories_tree());        // 分类树
            \Tpl::assign('helps', get_shop_help());              // 网店帮助
            \Tpl::assign('top_goods', get_top10());                  // 销售排行
            \Tpl::assign('show_marketprice', $_CFG['show_marketprice']);
            \Tpl::assign('brand_cat_list', brand_related_cat($brand_id)); // 相关分类
            \Tpl::assign('feed_url', ($_CFG['rewrite'] == 1) ? "feed-b$brand_id.xml" : 'feed.php?brand=' . $brand_id);

            /* 调查 */
            $vote = get_vote();
            if (!empty($vote)) {
                \Tpl::assign('vote_id', $vote['id']);
                \Tpl::assign('vote', $vote['content']);
            }

            \Tpl::assign('best_goods', brand_recommend_goods('best', $brand_id, $cate));
            \Tpl::assign('promotion_goods', brand_recommend_goods('promote', $brand_id, $cate));
            \Tpl::assign('brand', $brand_info);
            \Tpl::assign('promotion_info', get_promotion_info());

            $count = goods_count_by_brand($brand_id, $cate);

            $goodslist = brand_get_goods($brand_id, $cate, $size, $page, $sort, $order);

            if ($display == 'grid') {
                if (count($goodslist) % 2 != 0) {
                    $goodslist[] = array();
                }
            }
            \Tpl::assign('goods_list', $goodslist);
            \Tpl::assign('script_name', 'brand');

            assign_pager('brand', $cate, $count, $size, $sort, $order, $page, '', $brand_id, 0, 0, $display); // 分页
            assign_dynamic('brand'); // 动态内容
        }

        \Tpl::display('brand.dwt', $cache_id);

        return view('shop::index');
    }

}


/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 获得指定品牌的详细信息
 *
 * @access  private
 * @param   integer $id
 * @return  void
 */
function get_brand_info($id)
{
    $sql = 'SELECT * FROM ' . \ECS::table('brand') . " WHERE brand_id = '$id'";

    return \Ecdb::getRow($sql);
}

/**
 * 获得指定品牌下的推荐和促销商品
 *
 * @access  private
 * @param   string  $type
 * @param   integer $brand
 * @return  array
 */
function brand_recommend_goods($type, $brand, $cat = 0)
{
    static $result = null;

    $time = gmtime();

    if ($result === null) {
        if ($cat > 0) {
            $cat_where = "AND " . get_children($cat);
        } else {
            $cat_where = '';
        }

        $sql = 'SELECT g.goods_id, g.goods_name, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
            "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, " .
            'promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, ' .
            'b.brand_name, g.is_best, g.is_new, g.is_hot, g.is_promote ' .
            'FROM ' . \ECS::table('goods') . ' AS g ' .
            'LEFT JOIN ' . \ECS::table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
            'LEFT JOIN ' . \ECS::table('member_price') . ' AS mp ' .
            "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
            "WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.brand_id = '$brand' AND " .
            "(g.is_best = 1 OR (g.is_promote = 1 AND promote_start_date <= '$time' AND " .
            "promote_end_date >= '$time')) $cat_where" .
            'ORDER BY g.sort_order, g.last_update DESC';
        $result = \Ecdb::getAll($sql);
    }

    /* 取得每一项的数量限制 */
    $num = 0;
    $type2lib = array('best' => 'recommend_best', 'new' => 'recommend_new', 'hot' => 'recommend_hot', 'promote' => 'recommend_promotion');
    $num = get_library_number($type2lib[ $type ]);

    $idx = 0;
    $goods = array();
    foreach ($result AS $row) {
        if ($idx >= $num) {
            break;
        }

        if (($type == 'best' && $row['is_best'] == 1) ||
            ($type == 'promote' && $row['is_promote'] == 1 &&
                $row['promote_start_date'] <= $time && $row['promote_end_date'] >= $time)
        ) {
            if ($row['promote_price'] > 0) {
                $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
                $goods[ $idx ]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
            } else {
                $goods[ $idx ]['promote_price'] = '';
            }

            $goods[ $idx ]['id'] = $row['goods_id'];
            $goods[ $idx ]['name'] = $row['goods_name'];
            $goods[ $idx ]['brief'] = $row['goods_brief'];
            $goods[ $idx ]['brand_name'] = $row['brand_name'];
            $goods[ $idx ]['short_style_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
            $goods[ $idx ]['market_price'] = price_format($row['market_price']);
            $goods[ $idx ]['shop_price'] = price_format($row['shop_price']);
            $goods[ $idx ]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
            $goods[ $idx ]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
            $goods[ $idx ]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);

            $idx++;
        }
    }

    return $goods;
}

/**
 * 获得指定的品牌下的商品总数
 *
 * @access  private
 * @param   integer $brand_id
 * @param   integer $cate
 * @return  integer
 */
function goods_count_by_brand($brand_id, $cate = 0)
{
    $sql = 'SELECT COUNT(*) FROM ' . \ECS::table('goods') . ' AS g ' .
        "WHERE brand_id = '$brand_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0";

    if ($cate > 0) {
        $sql .= " AND " . get_children($cate);
    }

    return \Ecdb::getOne($sql);
}

/**
 * 获得品牌下的商品
 *
 * @access  private
 * @param   integer $brand_id
 * @return  array
 */
function brand_get_goods($brand_id, $cate, $size, $page, $sort, $order)
{
    $cate_where = ($cate > 0) ? 'AND ' . get_children($cate) : '';

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.market_price, g.shop_price AS org_price, ' .
        "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, " .
        'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .
        'FROM ' . \ECS::table('goods') . ' AS g ' .
        'LEFT JOIN ' . \ECS::table('member_price') . ' AS mp ' .
        "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
        "WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.brand_id = '$brand_id' $cate_where" .
        "ORDER BY $sort $order";

    $res = \Ecdb::selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = \Ecdb::fetchRow($res)) {
        if ($row['promote_price'] > 0) {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        } else {
            $promote_price = 0;
        }

        $arr[ $row['goods_id'] ]['goods_id'] = $row['goods_id'];
        if ($GLOBALS['display'] == 'grid') {
            $arr[ $row['goods_id'] ]['goods_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'],
                $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        } else {
            $arr[ $row['goods_id'] ]['goods_name'] = $row['goods_name'];
        }
        $arr[ $row['goods_id'] ]['market_price'] = price_format($row['market_price']);
        $arr[ $row['goods_id'] ]['shop_price'] = price_format($row['shop_price']);
        $arr[ $row['goods_id'] ]['promote_price'] = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[ $row['goods_id'] ]['goods_brief'] = $row['goods_brief'];
        $arr[ $row['goods_id'] ]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[ $row['goods_id'] ]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[ $row['goods_id'] ]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
    }

    return $arr;
}

/**
 * 获得与指定品牌相关的分类
 *
 * @access  public
 * @param   integer $brand
 * @return  array
 */
function brand_related_cat($brand)
{
    $arr[] = array('cat_id'   => 0,
                   'cat_name' => $GLOBALS['_LANG']['all_category'],
                   'url'      => build_uri('brand', array('bid' => $brand), $GLOBALS['_LANG']['all_category']));

    $sql = "SELECT c.cat_id, c.cat_name, COUNT(g.goods_id) AS goods_count FROM " .
        \ECS::table('category') . " AS c, " .
        \ECS::table('goods') . " AS g " .
        "WHERE g.brand_id = '$brand' AND c.cat_id = g.cat_id " .
        "GROUP BY g.cat_id";
    $res = \Ecdb::query($sql);

    while ($row = \Ecdb::fetchRow($res)) {
        $row['url'] = build_uri('brand', array('cid' => $row['cat_id'], 'bid' => $brand), $row['cat_name']);
        $arr[] = $row;
    }

    return $arr;
}